<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Report_laporan_penerimaan extends Authenticated_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('laporan_setoran_model', 'lap');
		$this->load->model('pencabang_model', 'cabang');
	}

	function index() {
		$this->list_result('', '', '', '');
	}

	function list_result($tgl_awal='', $tgl_akhir='') {
		$data['filter_cabang'] = $this->cabang->get_all_cabang();

		if ($tgl_awal == '') {
			$data['tgl_awal'] = date('m/d/Y');
		} else {
			$data['tgl_awal'] = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'm/d/Y');
		}
		if ($tgl_akhir == '') {
			$data['tgl_akhir'] = date('m/d/Y');
		} else {
			$data['tgl_akhir'] = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'm/d/Y');
		}

		$data['list_result'] = $this->lap->get_laporan_penerimaan($tgl_awal, $tgl_akhir);
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$data['kode_coa'][$i] = $this->lap->get_kode_coa($data['list_result'][$i]['asal'], $data['list_result'][$i]['tujuan']);
			$data['jumlah'][$i] = $this->lap->get_biaya_langsung_by_SPJ($data['list_result'][$i]['NoSPJ']);
		}
		$this->load->view('template/header_view');
		$this->load->view('laporan/laporan_penerimaan', $data);
		$this->load->view('template/footer_view');
	}

	function cetak_pdf($tgl_awal, $tgl_akhir, $cabang) {
		if ($tgl_awal == '') {
			$data['tgl_awal'] = date('m/d/Y');
		} else {
			$data['tgl_awal'] = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'm/d/Y');
		}
		if ($tgl_akhir == '') {
			$data['tgl_akhir'] = date('m/d/Y');
		} else {
			$data['tgl_akhir'] = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'm/d/Y');
		}
		$data['cabang']=$cabang;
		$data['list_result'] = $this->lap->get_laporan_penerimaan_cabang($tgl_awal, $tgl_akhir,$cabang);
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$data['kode_coa'][$i] = $this->lap->get_kode_coa($data['list_result'][$i]['asal'], $data['list_result'][$i]['tujuan']);
			$data['jumlah'][$i] = $this->lap->get_biaya_langsung_by_SPJ($data['list_result'][$i]['NoSPJ']);
		}
		$html = $this->load->view('laporan/html_cetak_laporan_penerimaan', $data,TRUE);
		$this->load->library('pdf');
		$this->pdf->to_pdf_landscape($html,'Report Laporan Penerimaan.pdf');
	}
	
	function cetak_excel($tgl_awal, $tgl_akhir, $cabang){
		$this->load->library('excel');
		
		if ($tgl_awal == '') {
			$data['tgl_awal'] = date('m/d/Y');
		} else {
			$data['tgl_awal'] = $this->date_func->change_format_date($tgl_awal, 'd-m-Y', 'm/d/Y');
		}
		if ($tgl_akhir == '') {
			$data['tgl_akhir'] = date('m/d/Y');
		} else {
			$data['tgl_akhir'] = $this->date_func->change_format_date($tgl_akhir, 'd-m-Y', 'm/d/Y');
		}
		$data['cabang']=$cabang;
		$data['list_result'] = $this->lap->get_laporan_penerimaan_cabang($tgl_awal, $tgl_akhir,$cabang);
		for ($i = 0; $i < count($data['list_result']); $i++) {
			$data['kode_coa'][$i] = $this->lap->get_kode_coa($data['list_result'][$i]['asal'], $data['list_result'][$i]['tujuan']);
			$data['jumlah'][$i] = $this->lap->get_biaya_langsung_by_SPJ($data['list_result'][$i]['NoSPJ']);
		}
		
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle('title')->setDescription('description');

		$objPHPExcel->setActiveSheetIndex(0);
		//Set Keterangan
		$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Laporan Penerimaan');
		$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Periode');
		$objPHPExcel->getActiveSheet()->setCellValue('B2', $data['tgl_awal'].'-'.$data['tgl_akhir']);
		$objPHPExcel->getActiveSheet()->setCellValue('A3', 'Cabang');
		$objPHPExcel->getActiveSheet()->setCellValue('B3', $cabang);
		//Set header
		$objPHPExcel->getActiveSheet()->setCellValue('A4', 'No.');
		$objPHPExcel->getActiveSheet()->setCellValue('B4', 'Tanggal Setoran');
		$objPHPExcel->getActiveSheet()->setCellValue('C4', 'No. SPJ');
		$objPHPExcel->getActiveSheet()->setCellValue('D4', 'Jurusan');
		$objPHPExcel->getActiveSheet()->setCellValue('E4', 'Kode Jurusan');
		$objPHPExcel->getActiveSheet()->setCellValue('F4', 'No. Polisi');
		$objPHPExcel->getActiveSheet()->setCellValue('G4', 'Total Tiket');
		$objPHPExcel->getActiveSheet()->setCellValue('H4', 'Total TIket Void');
		$objPHPExcel->getActiveSheet()->setCellValue('I4', 'Total Omset');
		$objPHPExcel->getActiveSheet()->setCellValue('J4', 'Total Discount');
		$objPHPExcel->getActiveSheet()->setCellValue('K4', 'Total Fee Agen');
		$objPHPExcel->getActiveSheet()->setCellValue('L4', 'Total Void');
		$objPHPExcel->getActiveSheet()->setCellValue('M4', 'Total Biaya Langsung');
		$objPHPExcel->getActiveSheet()->setCellValue('N4', 'Total Setor');
		// Save it as an excel 2003 file
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		
		$row=5;
		
		for($i=0;$i<count($data['list_result']);$i++){
			
			$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, ($i+1));
			$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $data['list_result'][$i]['tglspj']);
			$objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $data['list_result'][$i]['NoSPJ']);
			$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $data['list_result'][$i]['asal'] . "-" . $data['list_result'][$i]['tujuan']);
			$objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $data['kode_coa'][$i]['kode_coa']);
			$objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $data['list_result'][$i]['NoPol']);
			$objPHPExcel->getActiveSheet()->setCellValue('G'.$row, $data['list_result'][$i]['total_penumpang']);
			$objPHPExcel->getActiveSheet()->setCellValue('H'.$row, $data['list_result'][$i]['tiket_void']);
			$objPHPExcel->getActiveSheet()->setCellValue('I'.$row, $data['list_result'][$i]['total_omset']);
			$objPHPExcel->getActiveSheet()->setCellValue('J'.$row, $data['list_result'][$i]['total_discount']);
			$objPHPExcel->getActiveSheet()->setCellValue('K'.$row, $data['list_result'][$i]['fee_agen']);
			$objPHPExcel->getActiveSheet()->setCellValue('L'.$row, $data['list_result'][$i]['total_void']);
			$objPHPExcel->getActiveSheet()->setCellValue('M'.$row, $data['jumlah'][$i]['jumlah']);
			$objPHPExcel->getActiveSheet()->setCellValue('N'.$row, $data['list_result'][$i]['total_setoran']);
		
			$row++;
		}
		$objWriter->save('public/Report Penerimaan '.$tgl_awal.'-'.$tgl_akhir.' '.$cabang.'.xls');
		redirect(PUBLIC_FOLDER.'Report Penerimaan '.$tgl_awal.'-'.$tgl_akhir.' '.$cabang.'.xls');
		
	}

}